package leetcode.sword;

class NumMatrix {

    public int[][] sums;
    public NumMatrix(int[][] matrix) {
        int row = matrix.length+1;
        if(row==1) return;
        int col = matrix[0].length+1;
        if(col==1) return;
        sums = new int[row][col];
        for(int i=1;i<row;i++){
            for(int j=1;j<col;j++){
                this.sums[i][j] = this.sums[i-1][j]+this.sums[i][j-1]-this.sums[i-1][j-1]+matrix[i-1][j-1];
            }
        }
    }

    public int sumRegion(int row1, int col1, int row2, int col2) {
        return this.sums[row2+1][col2+1]-this.sums[row1][col2+1]-this.sums[row2+1][col1]+this.sums[row1][col1];
    }

    public static void main(String[] args) {
//        int[][] matrix = {{3,0,1,4,2},{5,6,3,2,1},{1,2,0,1,5},{4,1,0,1,7},{1,0,3,0,5}};
//        int[][] matrix = {{}};
////        java.sword.NumMatrix obj = new java.sword.NumMatrix(matrix);
////        System.out.println(obj.sumRegion(2,1,4,3));
//        System.out.println(matrix.length);
    }
}

/**
 * Your java.sword.NumMatrix object will be instantiated and called as such:
 * java.sword.NumMatrix obj = new java.sword.NumMatrix(matrix);
 * int param_1 = obj.sumRegion(row1,col1,row2,col2);
 */